<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#">
  <!ENTITY dc "http://purl.org/dc/elements/1.1/">
  <!ENTITY dcterms "http://purl.org/dc/terms/">
  <!ENTITY foaf "http://xmlns.com/foaf/0.1/">
  <!ENTITY a "http://xmlns.heustics.org/schema/myelin/0.1/">
  <!ENTITY m "http://xmlns.heustics.org/ontology/myelin/0.1/">
]>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:date="http://exslt.org/dates-and-times" 
  xmlns:exslt="http://exslt.org/common"
  xmlns:a="http://xmlns.heustics.org/schema/myelin/0.1/" 
  xmlns:o="&a;default#"  
  xmlns:l="http://xmlns.heustics.org/schema/i18n/0.1/" 
  xmlns:sess="http://xmlns.heustics.org/xslt/ext/session/0.1/"
  xmlns:fres="http://xmlns.4suite.org/reserved"
  xmlns:url="http://xmlns.heustics.org/xslt/ext/urllib/0.1/"
  extension-element-prefixes="exslt date sess url"
  exclude-result-prefixes="xsl exslt date sess a o l fres url"
>
  <!-- xsl:import href="base.xslt"/ -->  

  <xsl:variable name="o:l10n" select="a:get-l10n('&a;default')"/>
    
  <xsl:template match="*" mode="a:main-meta"/>
  
  <xsl:template match="a:head" mode="a:main-head">
    
    <div id="main-actions" class='page-section actions actions-section'>
      <xsl:apply-templates mode="a:main-actions"/>
      <div class='spacer'/>
    </div>  
    
    <div id='main-header' class='page-section header-section'>      
      <xsl:apply-templates mode="a:main-header"/>
      <div class="spacer"/>
    </div>  
    
    <xsl:apply-templates select="." mode="a:main-meta-wrapper"/>
    
    <div id="main-nav" class='page-section nav-section'>
      <xsl:apply-templates mode="a:main-nav"/>
      <div class="spacer"/>
    </div>
    
  </xsl:template> 
  
  <xsl:template match="*" mode="a:main-meta-wrapper">  
    <div id="main-meta" class='page-section meta-section'>
      <xsl:apply-templates mode="a:main-meta">
        <xsl:with-param name="l10n" select="$o:l10n/l:context[@name='meta']/*"/>
      </xsl:apply-templates>
      <div class="spacer"/>
    </div>        
  </xsl:template>   
  
  <!-- BEGIN APPLY-TEMPLATES  -->
  
  <xsl:template match="a:title" mode="a:main-header">
    <xsl:param name="config" select="$a:config"/>
    <xsl:param name="account" select="$a:account"/>
    <xsl:param name="action" select="/*/@action"/>
    <div class="header">      
      <h1>
        <xsl:value-of select="."/>  
      </h1>
      <div class="spacer"/>    
    </div>
  </xsl:template>
  
  <xsl:template match="a:meta" mode="a:main-meta">   
    <xsl:param name="l10n" select="$o:l10n/l:context[@name='meta']/*"/>
    <div class="meta">
      <xsl:call-template name="a:lang-attr"/>
      <div class="entry node-id">
        <div class="key">
          <xsl:value-of select="$o:l10n/l:gentext[@key='node-id']"/>
          <div class="spacer"/>
        </div>
        <div class="value">
          <span><xsl:value-of select="$a:node-id"/></span>
          <div class="spacer"/>
        </div>
        <div class="spacer"/>
      </div>
      <xsl:apply-templates select="a:language" mode="a:meta">
        <xsl:with-param name="l10n" select="$l10n"/>
      </xsl:apply-templates>
      <xsl:apply-templates select="a:created|a:creator|a:modified|a:last-modifier" mode="a:meta">
        <xsl:with-param name="l10n" select="$l10n"/>
      </xsl:apply-templates>
      <xsl:apply-templates select="a:keywords" mode="a:meta">
        <xsl:with-param name="l10n" select="$l10n"/>
      </xsl:apply-templates>
      <div class="spacer"/>
    </div>
  </xsl:template>
  
  <xsl:template match="a:nav" mode="a:main-nav">
    <xsl:param name="nav-l10n" select="$o:l10n/l:context[@name='navigation']/*"/>    
    <div class='navigation'>                
      <div id='main_link' class="nav">
        <a href="{a:format-root-action()}">
          <xsl:value-of select="$nav-l10n[@key='main']"/>
        </a>
        <div class="spacer"/>
      </div> 
      <xsl:apply-templates mode="a:nav">
        <xsl:with-param name="nav-l10n" select="$nav-l10n"/>
      </xsl:apply-templates>    
      <div class="spacer"/>      
    </div>   
  </xsl:template>
  
  <xsl:template match="a:account" mode="a:main-actions">
    <xsl:param name="config" select="$a:config"/>
    <xsl:param name="action" select="$a:action"/>
    <xsl:param name="actions-l10n" select="$o:l10n/l:context[@name='actions']/*"/>    
    <xsl:variable name="node-acls" select="a:node-acls"/>
    <xsl:if test="a:is-not-action('search')">       
      <div id='search' class="box action">     
        <xsl:apply-templates select="$node-acls" mode="a:search-form">
          <xsl:with-param name="config" select="$config"/>
          <xsl:with-param name="account" select="."/>
        </xsl:apply-templates>
      </div>
    </xsl:if>  
    <xsl:if test="$config/@show-anonymous-profile">
      <div id='profile' class="box action">
        <dl>
          <dt>
            <xsl:value-of select="$actions-l10n[@key='profile']"/>
          </dt>
        </dl>
        <div class='body'>
          <div class='wrapper'>
            <xsl:apply-templates select="a:profile" mode="a:actions">
              <xsl:with-param name="l10n" select="$o:l10n/l:context[@name='profile-actions']/*"/>
              <xsl:with-param name="config" select="$config"/>
              <xsl:with-param name="node-acls" select="$node-acls"/>
            </xsl:apply-templates>
          </div>
          <div class='spacer profile-body-spacer'/> 
        </div>
        <div id='profile-spacer' class='spacer profile-action-spacer'/>           
      </div>
    </xsl:if>
    <xsl:variable name="toolbox-rtf">
      <xsl:apply-templates select="$node-acls" mode="a:actions">
        <xsl:with-param name="config" select="$config"/>
        <xsl:with-param name="action" select="$action"/>
      </xsl:apply-templates>
    </xsl:variable>
    <xsl:variable name="toolbox" select="exslt:node-set($toolbox-rtf)/*"/>
    <xsl:if test="count($toolbox)">
      <div id='toolbox' class='box action'>            
        <dl>
          <dt>
            <xsl:value-of select="$actions-l10n[@key='actions']"/>
          </dt>
        </dl>  
        <div class='body'>
          <div class='wrapper'>
            <xsl:copy-of select="$toolbox"/>
          </div>
          <div class='spacer toolbox-body-spacer'/> 
        </div>
        <div id='toolbox-spacer' class='spacer toolbox-action-spacer'/>             
      </div>    
    </xsl:if>      
  </xsl:template>
    
  <xsl:template match="a:account[@logged-in]" mode="a:main-actions">
    <xsl:param name="config" select="$a:config"/>
    <xsl:param name="action" select="$a:action"/>
    <xsl:param name="actions-l10n" select="$o:l10n/l:context[@name='actions']/*"/>    
    <xsl:variable name="node-acls" select="a:node-acls"/>
    <xsl:if test="a:is-not-action('search')">       
      <div id='search' class='box action'>     
        <xsl:apply-templates select="$node-acls" mode="a:search-form">
          <xsl:with-param name="config" select="$config"/>
          <xsl:with-param name="account" select="."/>
        </xsl:apply-templates>
      </div>
    </xsl:if>  
    <div id='profile' class='box action'>
      <dl>
        <dt>
          <xsl:value-of select="$actions-l10n[@key='profile']"/>
        </dt>
      </dl>
      <div class='body'>
        <div class='wrapper'>
          <xsl:apply-templates select="a:profile" mode="a:actions">
            <xsl:with-param name="l10n" select="$o:l10n/l:context[@name='profile-actions']/*"/>
            <xsl:with-param name="config" select="$config"/>
            <xsl:with-param name="node-acls" select="$node-acls"/>
          </xsl:apply-templates>
        </div>
        <div class="spacer profile-body-spacer"/>
      </div> 
      <div id='profile-spacer' class='spacer profile-action-spacer'/>           
    </div>
    <xsl:variable name="toolbox-rtf">
      <xsl:apply-templates select="$node-acls" mode="a:actions">
        <xsl:with-param name="config" select="$config"/>
        <xsl:with-param name="action" select="$action"/>
      </xsl:apply-templates>
    </xsl:variable>
    <xsl:variable name="toolbox" select="exslt:node-set($toolbox-rtf)/*"/>
    <xsl:if test="count($toolbox)">
      <div id='toolbox' class='box action'>            
        <dl>
          <dt>
            <xsl:value-of select="$actions-l10n[@key='actions']"/>
          </dt>
        </dl>  
        <div class='body'>
          <div class='wrapper'>
            <xsl:copy-of select="$toolbox"/>
          </div> 
          <div class='spacer toolbox-body-spacer'/>
        </div>
        <div id='toolbox-spacer' class='spacer'/>             
      </div>    
    </xsl:if>      
  </xsl:template>
  
  <!-- toolbox actions -->
  <xsl:template match="a:node-acls" mode="a:actions">  
    <xsl:param name="config" select="$a:config"/>
    <xsl:param name="action" select="$a:action"/>
    <xsl:apply-templates mode="a:node-action">
      <xsl:with-param name="config" select="$config"/>
      <xsl:with-param name="action" select="$action"/>
    </xsl:apply-templates>
  </xsl:template>  
  
  <xsl:template match="*" mode="a:node-action"/>
  
  <xsl:template match="a:profile" mode="a:actions">
    <xsl:param name="config" select="$a:config"/>
    <xsl:param name="profile-l10n" select="$o:l10n/l:context[@name='profile-actions']/*"/>        
    <div id='profile-logged-in-as'>
      <span>
        <xsl:value-of select="$profile-l10n[@key='guest-user']"/>
      </span>
      <div class="spacer"/>
    </div>
    <div id='profile-preferences'>
      <a href="{a:format-root-action('edit-preferences')}">
        <xsl:value-of select="$profile-l10n[@key='edit-preferences']"/>
      </a>
      <div class="spacer"/>
    </div>    
    <div id='profile-login'>
      <a href="{a:format-url-full-secure(a:format-root-action('login'))}">
        <xsl:value-of select="$profile-l10n[@key='login']"/>
      </a>
      <div class="spacer"/>
    </div>   
    <!-- xsl:if test="$config/@show-anonymous-registration">
      <div>      
        <a href="{a:format-url-full-secure(a:format-root-action('join'))}">
          <xsl:value-of select="$profile-l10n[@key='join']"/>
        </a>
        <div class="spacer"/>
      </div> 
      <div>      
        <a href="{a:format-url-full-secure(a:format-root-action('activate'))}">
          <xsl:value-of select="$profile-l10n[@key='activate']"/>
        </a>
        <div class="spacer"/>
      </div> 
    </xsl:if -->
  </xsl:template>    
  
  <xsl:template match="a:profile[@agent-id]" mode="a:actions">
    <xsl:param name="profile-l10n" select="$o:l10n/l:context[@name='profile-actions']/*"/>    
    
    <div id='profile-logged-in-as'>
      <span><xsl:value-of select="@nick"/></span>
      <div class="spacer"/>
    </div>  
     
    <!-- xsl:if test="@domain-id">
      <div id='profile-view'>
        <a href="{a:format-root-action('create-user-profile',a:public-url(@domain-id))}">
          <xsl:value-of select="$profile-l10n[@key='create-user-profile']"/>
        </a>     
      </div>
    </xsl:if -->
    
    <div id='profile-logoff'>
      <a href="{a:format-url-ext(
        a:format-url-ext(
          a:format-root-action('login'),
          'event','logoff'
        ),
        'logoff','logoff')}">
        <xsl:value-of select="$profile-l10n[@key='logoff']"/>
      </a>
      <div class="spacer"/>
    </div>
  </xsl:template>      
  
  <xsl:template match="a:profile[@node-id]" mode="a:actions">
    <xsl:param name="profile-l10n" select="$o:l10n/l:context[@name='profile-actions']/*"/>    
    <xsl:variable name="node-url" select="a:public-url(@uri)"/>
    <div id='profile-logged-in-as'>
      <span><xsl:value-of select="@nick"/></span>
      <div class="spacer"/>
    </div>  
     
    <div id='profile-view'>
      <xsl:choose>
        <xsl:when test="a:is-not-current-node(@node-id) or a:is-not-action('view')">
          <a href="{a:format-url-min($node-url,'view')}">
            <xsl:value-of select="$profile-l10n[@key='view-profile']"/>
          </a>     
        </xsl:when>
        <xsl:otherwise>
          <span>
            <xsl:value-of select="$profile-l10n[@key='view-profile']"/>
          </span>
        </xsl:otherwise>
      </xsl:choose>
      <div class="spacer"/>     
    </div>   
    
    <div id='profile-preferences'>
      <a href="{a:format-request('edit-preferences',$node-url)}">
        <xsl:value-of select="$profile-l10n[@key='edit-preferences']"/>
      </a>
      <div class="spacer"/>
    </div>    
    <div id='profile-logoff'>
      <a href="{a:format-url-ext(
        a:format-url-ext(
          a:format-root-action('login'),
          'event','logoff'
        ),
        'logoff','logoff')}">
        <xsl:value-of select="$profile-l10n[@key='logoff']"/>
      </a>
      <div class="spacer"/>
    </div>
  </xsl:template>   
  
  <!-- nav -->
  <xsl:template match="*" mode="a:nav"/>
  
  <xsl:template match="a:parents[*[@node-id!=$a:config/@root-id]]|a:node-groupings[*]" mode="a:nav">
    <xsl:param name="nav-l10n" select="$o:l10n/l:context[@name='navigation']/*"/>
    <xsl:param name="key" select="local-name()"/>
    <div class="nav {$key}"> 
      <div class="node">   
        <div class='key'>       
          <h3>
            <xsl:value-of select="$nav-l10n[@key=$key]"/>
          </h3>
          <div class='spacer'/>
        </div>
        <div class='value'>      
          <ul>
            <xsl:for-each select="*">
              <li class='parent'>
                <a href='{a:format-url(a:public-url(@uri))}' title='{@tag}'>
                  <xsl:value-of select='@title'/>
                </a>
              </li>
            </xsl:for-each>        
          </ul>
          <div class="spacer"/>
        </div>
      </div>      
      <div class="spacer"/>        
    </div>
  </xsl:template>    
  
  <xsl:template match="a:translations[*]" mode="a:nav">
    <xsl:param name="nav-l10n" select="$o:l10n/l:context[@name='navigation']/*"/>
    <div class="nav translations"> 
      <div class="key">          
        <h3>
          <xsl:value-of select="$nav-l10n[@key='translations']"/>
        </h3>
        <div class='spacer'/>
      </div>
      <div class='value'>      
        <ul>
          <xsl:for-each select="*">
            <li class="parent">    
              <xsl:choose>
                <xsl:when test="@selected">
                  <span class="selected">
                    <xsl:choose>
                      <xsl:when test="@original">
                        <xsl:attribute name="class">selected source</xsl:attribute>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:attribute name="class">selected</xsl:attribute>
                      </xsl:otherwise>
                    </xsl:choose>
                    <xsl:value-of select="@title"/>
                  </span>
                </xsl:when>
                <xsl:otherwise>
                  <a href='{a:format-url(a:public-url(@uri))}'>
                    <xsl:choose>
                      <xsl:when test="@original">
                        <xsl:attribute name="class">source</xsl:attribute>
                        <xsl:attribute name="title">
                          <xsl:value-of select="concat($nav-l10n[@key='i18n-source'],@locale)"/>
                        </xsl:attribute>                   
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:attribute name="title">
                          <xsl:value-of select="@locale"/>
                        </xsl:attribute>            
                      </xsl:otherwise>
                    </xsl:choose>
                    <xsl:value-of select='@title'/>
                  </a>
                </xsl:otherwise>
              </xsl:choose>    
              <div class='spacer'/>
            </li>
          </xsl:for-each>            
        </ul>
      </div>             
      <div class="spacer"/>        
    </div>
  </xsl:template>  
  
  <!-- meta -->
  <xsl:template name="a:main-meta">
    <xsl:param name="l10n" select="$o:l10n/l:context[@name='meta']/*"/>
    <xsl:param name="key" select="local-name()"/>
    <div class="entry {$key}">
      <div class="key">
        <span><xsl:value-of select="$l10n[@key=$key]"/></span>
        <div class="spacer"/>
      </div>
      <div class="value">
        <span><xsl:value-of select="."/></span>
        <div class="spacer"/>
      </div>
      <div class="spacer"/>
    </div>
  </xsl:template>
  
  <xsl:template match="a:language" mode="a:meta">
    <xsl:param name="l10n" select="$o:l10n/l:context[@name='meta']/*"/>
    <div class="entry language">
      <div class="key">
        <span><xsl:value-of select="$l10n[@key='language']"/></span>
        <div class="spacer"/>
      </div>
      <div class="value">
        <span><xsl:value-of select="."/></span>
        <div class="spacer"/>
      </div>
      <div class="spacer"/>
    </div>
  </xsl:template>
  
  <xsl:template match="a:created|a:modified" mode="a:meta">
    <xsl:param name="l10n" select="$o:l10n/l:context[@name='meta']/*"/>
    <xsl:param name="key" select="local-name()"/>
    <xsl:if test="string-length(.)">
      <div class="entry {$key}">
        <div class="key">
          <span><xsl:value-of select="$l10n[@key=$key]"/></span>
          <div class="spacer"/>
        </div>
        <div class="value">
          <span><xsl:value-of select="a:format-date(string(.))"/></span>
          <div class="spacer"/>
        </div>
        <div class="spacer"/>
      </div>
    </xsl:if>    
  </xsl:template> 
  
  <xsl:template match="a:creator|a:last-modifier" mode="a:meta">
    <xsl:param name="l10n" select="$o:l10n/l:context[@name='meta']/*"/>
    <xsl:param name="key" select="local-name()"/>
    <xsl:if test="@node-id">
      <div class="entry {$key}">
        <div class="key">
          <span><xsl:value-of select="$l10n[@key=$key]"/></span>
          <div class="spacer"/>
        </div>
        <div class="value">
          <xsl:variable name="profile" select="a:profile"/>
          <xsl:choose>
            <xsl:when test="$profile">
              <a href="{a:format-url(a:public-url($profile/@uri))}" title="{@name}">
                <xsl:choose>
                  <xsl:when test="@nick">
                    <xsl:value-of select="@nick"/>
                  </xsl:when>
                  <xsl:when test="@name">
                    <xsl:value-of select="@name"/>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:value-of select="$l10n[@key='unknown-user']"/>
                  </xsl:otherwise>
                </xsl:choose>
              </a>
            </xsl:when>
            <xsl:otherwise>
              <xsl:choose>
                <xsl:when test="@nick">
                  <xsl:value-of select="@nick"/>
                </xsl:when>
                <xsl:when test="@name">
                  <xsl:value-of select="@name"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="$l10n[@key='unknown-user']"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:otherwise>
          </xsl:choose>
          <span><xsl:value-of select="."/></span>
          <div class="spacer"/>
        </div>
        <div class="spacer"/>
      </div>
    </xsl:if>  
  </xsl:template>
  
  <xsl:template match="a:keywords" mode="a:meta">
    <xsl:param name="l10n" select="$o:l10n/l:context[@name='meta']/*"/>
    <xsl:param name="key" select="local-name()"/>
    <xsl:if test="*">
      <div class="entry keywords">
        <div class="key">
          <span><xsl:value-of select="$l10n[@key=$key]"/></span>
          <div class="spacer"/>
        </div>
        <div class="value">
          <xsl:variable name="uri" select="$fres:config-doc/a:QuickSearchServices/a:Service[@type='&m;TextSearchService']"/>
          <xsl:choose>
            <xsl:when test="$uri">
              <xsl:variable name="url" select="a:format-node-uri($uri[1]/@uri,'query')"/>
              <xsl:for-each select="*">
                <a href="{a:format-keyword-search($url,.)}" title="{$l10n[@key='keyword-search']}">
                  <xsl:value-of select="."/>
                </a>
                <xsl:if test="position() &lt; last()">
                  <xsl:value-of select="$l10n[@key='keyword-separator']"/>
                </xsl:if>          
              </xsl:for-each>
            </xsl:when>
            <xsl:otherwise>
              <xsl:for-each select="*">
                <span>
                  <xsl:value-of select="."/>
                </span>
                <xsl:if test="position() &lt; last()">
                  <xsl:value-of select="$l10n[@key='keyword-separator']"/>
                </xsl:if>          
              </xsl:for-each>
            </xsl:otherwise>
          </xsl:choose>
          <div class="spacer"/>
        </div>
        <div class="spacer"/>
      </div>
    </xsl:if>
  </xsl:template>
  
  <!-- meta -->
  <xsl:template match="a:meta" mode="a:head-content">
    <xsl:apply-templates mode="a:head-meta-tags">
      <xsl:with-param name="lang" select="string(@xml:lang)"/>
    </xsl:apply-templates>
  </xsl:template>
  
  <xsl:template match="*" mode="a:head-meta-tags"/>
  
  <xsl:template match="a:description" mode="a:head-meta-tags">
    <xsl:param name="lang"/>
    <meta name="description" content="{.}">
      <xsl:call-template name="a:lang-attr">
        <xsl:with-param name="lang" select="$lang"/>
      </xsl:call-template>
    </meta>   
  </xsl:template>
  
  <xsl:template match="a:language" mode="a:head-meta-tags">
    <meta http-equiv="content-language" content="{.}"/>
  </xsl:template>
  
  <!-- this is not correct semantics
       the creator of the *HTML* webpage is not the same 
    xsl:template match="a:creator[@name]" mode="a:head-meta-tags">
    <meta name="author" content="{@name}"/>
  </xsl:template -->
  
  <xsl:template match="a:keywords[*]" mode="a:head-meta-tags">
    <xsl:param name="lang"/>
    <meta name="keywords">
      <xsl:call-template name="a:lang-attr">
        <xsl:with-param name="lang" select="$lang"/>
      </xsl:call-template>
      <xsl:attribute name="content">
        <xsl:for-each select="*[position() &lt; last()]">
          <xsl:value-of select="concat(.,',')"/>
        </xsl:for-each>
        <xsl:value-of select="*[position() = last()]"/>
      </xsl:attribute>
    </meta>    
  </xsl:template>
  
</xsl:stylesheet>

